﻿using AgencyManagerSystem.AsmType;
using AgencyManagerSystem.Common;
using AgencyManagerSystem.Core;
using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace AgencyManagerSystem.Controllers
{
    public class RetailController : MyControllerBase
    {
        RetailManager manager;
        RetailFetcher fetcher;
        RetailMemberFetcher memberFetcher;
        RetailMemberManager memberManager;
        public RetailController() : base()
        {
            this.manager = new RetailManager();
            this.fetcher = new RetailFetcher();
            this.memberFetcher = new RetailMemberFetcher();
            this.memberManager = new RetailMemberManager();
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Center, AppOpt.Create)]
        public AsmObject<RetailModel> Set(RetailModel supplierModel)
        {
            if (supplierModel == null)
            {
                DBHelper.Commit( false);
                LogHelper.Err(this.GetType(), String.Format("SET:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return new AsmObject<RetailModel>() { Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
            }
            LogHelper.Debug(this.GetType(), String.Format("SET:[RQS|{1}]{0}", Util.ConvertToJson(supplierModel), RequestIp));
            var result = manager.Set(supplierModel);
            LogHelper.Debug(this.GetType(), String.Format("SET:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            DBHelper.Commit( true);
            return result;
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Center, AppOpt.Create)]
        public AsmArray<RetailMemberModel> SetMember(dynamic dataJson)
        {
            int retail = (Int32)dataJson.retail;
            string data = (String)dataJson.jsonRetailMemberModel;
            IEnumerable<RetailMemberModel> retailModels = Util.ConvertArrayFromJson<RetailMemberModel>(data);
            if (retailModels == null)
            {
                DBHelper.Commit( false);
                LogHelper.Err(this.GetType(), String.Format("SETMEMBER:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return new AsmArray<RetailMemberModel>() { Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
            }
            LogHelper.Debug(this.GetType(), String.Format("SET:[RQS|{1}]RetailId={2}:{0}", Util.ConvertToJson(retailModels), RequestIp, retail));
            var result = memberManager.UpdateList(retail, retailModels);
            LogHelper.Debug(this.GetType(), String.Format("SET:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            DBHelper.Commit( true);
            return result;
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Center, AppOpt.Delete)]
        public AsmObject<bool> Del(RetailModel retail)
        {
            if (retail == null)
            {
                DBHelper.Commit( false);
                LogHelper.Err(this.GetType(), String.Format("SET:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return new AsmObject<bool>() { Data = false, Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
            }
            LogHelper.Debug(this.GetType(), String.Format("DEL:[RQS|{1}]{0}", Util.ConvertToJson(retail), RequestIp));
            var result = manager.Delete(retail);
            LogHelper.Debug(this.GetType(), String.Format("DEL:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            DBHelper.Commit( true);
            return result;
        }
        [HttpGet]
        [Authorize]
        public AsmArray<RetailModel> List(string jsonCondition, int pos, int size)
        {
            LogHelper.Debug(this.GetType(), String.Format("LIST:[RQS|{1}]{0}", jsonCondition, RequestIp));
            int cnt = 0;
            var model = fetcher.List(Util.ConvertArrayFromJson<Condition>(jsonCondition), pos, size, out cnt);
            DBHelper.Commit( true);
            var result = new AsmArray<RetailModel>() { Data = model, Message = "操作成功", Result = AmsCode.Succ, Cnt = cnt };
            LogHelper.Debug(this.GetType(), String.Format("LIST:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [RoleFlagAuthorizeAttribute(PolicyDomain.Center, AppOpt.Exec)]
        public AsmArray<RetailMemberModel> ListMember(int id)
        {
            LogHelper.Debug(this.GetType(), String.Format("LISTMEMBERBYRETAIL:[RQS|{1}]{0}", id, RequestIp));
            int cnt = 0;
            var model = memberFetcher.ListByRetail(id);
            DBHelper.Commit(true);
            var result = new AsmArray<RetailMemberModel>() { Data = model, Message = "操作成功", Result = AmsCode.Succ, Cnt = cnt };
            LogHelper.Debug(this.GetType(), String.Format("LISTMEMBERBYRETAIL:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpGet]
        [Authorize]
        public AsmObject<RetailModel> Get(int id)
        {
            LogHelper.Debug(this.GetType(), String.Format("GET:[RQS|{1}]{0}", Util.ConvertToJson(id), RequestIp));
            var data = fetcher.Get(id);
            DBHelper.Commit( true);
            AsmObject<RetailModel> result = null;
            if (data != null)
                result = new AsmObject<RetailModel>() { Data = data, Message = "操作成功", Result = AmsCode.Succ };
            else
                result = new AsmObject<RetailModel>() { Data = data, Message = "无法找到数据", Result = AmsCode.DataNotFound };
            LogHelper.Debug(this.GetType(), String.Format("GET:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpGet]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Center, AppOpt.Exec)]
        public AsmArray<RetailModel> ListBySales()
        {
            var list = fetcher.ListBySales(profile.UserId);
            DBHelper.Commit( true);
            var result = new AsmArray<RetailModel>() { Data = list, Message = "操作成功", Result = AmsCode.Succ };
            LogHelper.Debug(this.GetType(), String.Format("LISTBYSALES:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Center, AppOpt.Delete)]
        public AsmArray<Int32> BulkDel(dynamic jsonRetailModel)
        {
            LogHelper.Debug(this.GetType(), String.Format("BULKDEL:[RQS|{1}]{0}", jsonRetailModel, RequestIp));
            var models = Util.ConvertArrayFromJson<RetailModel>((String)jsonRetailModel.data);
            if (models == null)
            {
                LogHelper.Err(this.GetType(), String.Format("BULKDEL:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (Int32)HttpStatusCode.BadRequest;
                var errResponse = new AsmArray<Int32>() { Data = new List<Int32>(), Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
                LogHelper.Debug(this.GetType(), String.Format("BULKDEL:[RSP|{1}]{0}", Util.ConvertToJson(errResponse), RequestIp));
                return errResponse;
            }
            int faild = models.Count();
            var del = new List<Int32>();
            StringBuilder sb = new StringBuilder();
            foreach (var model in models)
            {
                var eff = manager.Delete(model);
                if (eff.Result != 0)
                {
                    sb.AppendFormat("{0}:{1}\r\n", model.AutoId, eff.Message);
                }
                else
                {
                    del.Add(model.AutoId);
                    faild--;
                }
            }
            DBHelper.Commit(true);
            var succResponse = new AsmArray<Int32>() { Data = del, Message = "共有" + (models.Count() - faild) + "笔资料删除成功", Result = AmsCode.Succ };
            LogHelper.Debug(this.GetType(), String.Format("BULKDEL:[RSP|{1}]{0}", Util.ConvertToJson(succResponse), RequestIp));
            return succResponse;
        }
    }
}